<?php
require_once("../DAO/UserDAO.php");
require_once("../DAO/QuestionDAO.php");
require_once("../DAO/AnswerDAO.php");
require_once("../DAO/ParagraphDAO.php");
require_once("../Entities/Question.php");
require_once("../Entities/Answer.php");
require_once("../Entities/QuestionResult.php");
require_once("../DAO/TestDAO.php");
require_once("../DAO/NoteDAO.php");
require_once("../DAO/WordDAO.php");
require_once("../DAO/TestDetailDAO.php");
require_once("../Entities/Feedback.php");
require_once("../DAO/FeedbackDAO.php");
require_once("../Utility/WebSession.php");
require_once("../Utility/FunctionUtilities.php");
include("../Chart/libchart.php");

//type = 0 : get question,answer,paragraph
//type = 1 : save answers to sesssion
//type = 2 : get result of user test
//type = 3 : save feedback
//type = 4 : save note
//type = 5 : get note
//type = 6 : add word
//type = 7 : search word
if(isset($_REQUEST["type"]))
{
	$type = $_REQUEST["type"];
	$questionDao = new QuestionDAO();
	$answerDao = new AnswerDAO();
	$paragraphDao = new ParagraphDAO();
	$kqList = array();
	if($type == 0)
	{
		$id = $_REQUEST["id"]; // question id
		$index = $_REQUEST["index"];
		$question = $questionDao->GetQuestionById($id);
		$test = WebSession::GetSession("TestSession");
		if($test != null)
		{
			if($question != null)
			{
				$answers = $answerDao->GetAnswersByQuestionId($id);
				$kqList["Answers"] = FunctionUtilities::CheckAnswers($question->Id, $answers);
				$kqList["AnswerRight"] = $question->GetRightAnswers($answers);
				$kqList["MultiChoice"] = $question->IsMultiChoice($answers);
				$kqList["Question"] = $question;
				if($question->Paragraph == null)
				{
					$kqList["Paragraph"] = "";
				}
				else
				{
					$paragraph = $paragraphDao->GetParagraphById($question->Paragraph);
					if($paragraph != null)
					{
						$kqList["Paragraph"] = $paragraph;
					}
					else
					{
						$kqList["Paragraph"] = "";
					}
				}
				$kqList["next"] = (($index + 1) >= count($test->Details) ? -1 : $test->Details[$index + 1]->QuestionId);
				$kqList["prev"] = (($index - 1) < 0 ? -1 : $test->Details[$index - 1]->QuestionId);
				$kqList["result"] = true;
				WebSession::SetSession("CurrentQuestion", $index);
			}
			else
			{
				$kqList["result"] = false;
			}
		}
		else
		{
			$kqList["result"] = false;
		}
		echo json_encode($kqList);
	}
	if($type == 1)
	{
		$index = $_REQUEST["index"];
		$answerIds = $_REQUEST["answers"];
		$questionId = $_REQUEST["question"];
		$questionResult = WebSession::GetSession("QuestionResult");
		if($questionResult == null)
			$questionResult = array();
		$isHas = false;
		for($i = 0; $i < count($questionResult); $i++)
		{
			if($questionResult[$i]->QuestionId == $questionId)
			{
				$questionResult[$i]->AnswersList = $answerIds;
				$isHas = true;
				break;
			}
		}
		if($isHas == false)
		{
			$item = new QuestionResult();
			$item->QuestionId = $questionId;
			$item->AnswersList = $answerIds;
			$item->QuestionIndex = $index;
			$questionResult[] = $item;
		}
		WebSession::SetSession("QuestionResult", $questionResult);
	}
	if($type == 2)
	{
		$rightQuestion = 0;
		$rightMarks = 0;
		$questionResult = WebSession::GetSession("QuestionResult");
		$test = WebSession::GetSession("TestSession");
		$totalQuestions = count($test->Details);
		$totalMarks = $test->GetTotalMarks();
		if($test != null)
		{
			for($i = 0; $i < count($questionResult); $i++)
			{
				if($questionResult[$i]->CheckCorrect() == true)
				{
					$rightQuestion++;
					$rightMarks = $rightMarks + $test->Details[$questionResult[$i]->QuestionIndex]->Marks;
				}
			}
			$chart = new PieChart(500, 250);
			
			$dataSet = new XYDataSet();
			$dataSet->addPoint(new Point("Right Questions", $rightQuestion));
			$dataSet->addPoint(new Point("Wrong Questions", $totalQuestions - $rightQuestion));
			$chart->setDataSet($dataSet);
			
			$chart->setTitle("User Test Result Chart");
			$chart->render("../ChartImages/TestResultQuestionChart.png");
		}
	}
	if($type == 3)
	{
		$description = WebUtilities::GetQueryString("des", "");
		$questionId = WebUtilities::GetQueryString("id", 0);
//		$kqList = array();
		if($description == "" || $questionId == 0 || WebSession::CurrentUserNoReturn() == null)
		{
			$kqList["result"] = false;
		}
		else
		{
			$feedbackDAO = new FeedbackDAO();
			$feedback = new Feedback();
			$feedback->CreatedBy = WebSession::CurrentUserNoReturn()->UserId;
			$feedback->Description = $description;
			$feedback->IsFixed = 0;
			$feedback->Question = $questionId;
			$feedbackDAO->CreateFeedback($feedback);
			$kqList["result"] = true;
		}
		echo json_encode($kqList);
	}
	if($type == 4)
	{
		if(WebSession::CurrentUserNoReturn() == null)
		{
			$kqList['result'] = false;
		}
		else
		{
			$noteDAO = new NoteDAO();
			$note = new Note();
			$note->Content = WebUtilities::GetQueryString("note", "");
			$note->CreatedBy = WebSession::CurrentUserNoReturn()->UserId;
			$noteDAO->CreateNote($note);
			$kqList["result"] = true;
		}
		echo json_encode($kqList);
	}
	if($type == 5)
	{
		if(WebSession::CurrentUserNoReturn() == null)
		{
			$kqList['result'] = false;
		}
		else
		{
			$noteDAO = new NoteDAO();
			$index = WebUtilities::GetQueryString("index", 0);
			$notelist = $noteDAO->GetNotesByUser(WebSession::CurrentUserNoReturn()->UserId);
			if($index >= 0 && $index < count($notelist))
			{
				$kqList['content'] = $notelist[$index]->Content;
				$kqList["result"] = true;
			}
			else
			{
				$kqList['result'] = false;
			}
		}
		echo json_encode($kqList);
	}
	if($type == 6)
	{
		if(WebSession::CurrentUserNoReturn() == null)
		{
			$kqList['result'] = false;
		}
		else
		{
			$wordDAO = new WordDAO();
			$word = new Word();
			$word->Key = WebUtilities::GetQueryString("key", "");
			$word->Translate = WebUtilities::GetQueryString("trans", "");
			$word->TypeWord = WebUtilities::GetQueryString("typeWord", 0);
			$word->CreatedBy = WebSession::CurrentUserNoReturn()->UserId;
			if($wordDAO->CreateWord($word) != null)
			{
				$kqList['result'] = true;
			}
			else
			{
				$kqList['result'] = false;
			}
		}
		echo json_encode($kqList);
	}
	if($type == 7)
	{
		if(WebSession::CurrentUserNoReturn() == null)
		{
			$kqList['result'] = "not found content";
		}
		else
		{
			$wordDAO = new WordDAO();
			$item = $wordDAO->GetWordByKey(WebUtilities::GetQueryString("key", ""), WebSession::CurrentUserNoReturn()->UserId, WebUtilities::GetQueryString("typeWord", 0));
			if($item == null)
				$kqList['result'] = "not found content";
			else
				$kqList['result'] = $item->Translate;
		}
		echo json_encode($kqList);
	}
}


?>